package com.leetroll.media.remove;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
/**
 * @Author qdy
 * @Description
 * @Date 17:18 2024/3/16
 *
 * 图片抠图
 **/
public final class FileImageUtil {
    // 自定义的背景色，可以根据红绿蓝的平均值写
    public static int color_range = 120;


    private static BufferedImage rotate(Image src, int angel) {
        int src_width = src.getWidth(null);
        int src_height = src.getHeight(null);
        // calculate the new image size
        Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
                src_width, src_height)), angel);

        BufferedImage res = null;
        res = new BufferedImage(rect_des.width, rect_des.height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = res.createGraphics();
        res = g2.getDeviceConfiguration().createCompatibleImage(rect_des.width, rect_des.height, Transparency.TRANSLUCENT);
        g2.dispose();
        g2 = res.createGraphics();
        // transform
        g2.translate((rect_des.width - src_width) / 2,
                (rect_des.height - src_height) / 2);
        g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
        g2.setColor(new Color(255, 0, 0));
        g2.setStroke(new BasicStroke(1));
        g2.drawImage(src, null, null);
        g2.dispose();
        return res;
    }

    public static Rectangle CalcRotatedSize(Rectangle src, int angel) {
        // if angel is greater than 90 degree, we need to do some conversion
        if (angel >= 90) {
            if (angel / 90 % 2 == 1) {
                int temp = src.height;
                src.height = src.width;
                src.width = temp;
            }
            angel = angel % 90;
        }

        double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
        double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
        double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
        double angel_dalta_width = Math.atan((double) src.height / src.width);
        double angel_dalta_height = Math.atan((double) src.width / src.height);

        int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
                - angel_dalta_width));
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
                - angel_dalta_height));
        int des_width = src.width + len_dalta_width * 2;
        int des_height = src.height + len_dalta_height * 2;
        return new Rectangle(new Dimension(des_width, des_height));
    }

    /**
     * 顺时针旋转
     *
     * @param base64FileStr
     * @param rotate
     * @return
     * @throws IOException
     */
    public static String rotateImage(String base64FileStr, int rotate) throws IOException {
        if (StringUtils.isEmpty(base64FileStr)) {
            return "";
        }

        Base64.Decoder decoder = Base64.getDecoder();
        // Base64解码,对字节数组字符串进行Base64解码并生成文件
        final byte[] byt = decoder.decode(base64FileStr);
        for (int i = 0, len = byt.length; i < len; ++i) {
            // 调整异常数据
            if (byt[i] < 0) {
                byt[i] += 256;
            }
        }
        InputStream input = new ByteArrayInputStream(byt);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedImage src = ImageIO.read(input);
        BufferedImage des = rotate(src, rotate);
        ImageIO.write(des, "png", out);
        final Base64.Encoder encoder = Base64.getEncoder();
        final byte[] bytes = out.toByteArray();
        return encoder.encodeToString(bytes);
    }

    public static MultipartFile koutTu(@RequestParam MultipartFile file) {
        //读取待处理图片为BufferedImage对象
        BufferedImage image = getBufferedImage(file);
        // 高度
        int height = image.getHeight();
        // 宽度
        int width = image.getWidth();

        // 生产背景透明和内容透明的图片
        ImageIcon imageIcon = new ImageIcon(image);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        // 获取画笔
        Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
        // 绘制Image的图片
        g2D.drawImage(imageIcon.getImage(), 0, 0, null);

        int alpha = 255;
        String removeRgb = null;
        // 遍历Y轴的像素
        for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {
            // 遍历X轴的像素
            for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {
                int rgb = bufferedImage.getRGB(x, y);
                // 取图片边缘颜色作为对比对象
                if (y == bufferedImage.getMinY() && x == bufferedImage.getMinX()) {
                    removeRgb = convertRgb(rgb);
                }
                // 设置为透明背景
                if (removeRgb.equals(convertRgb(rgb))) {
                    alpha = 0;
                } else {
                    alpha = 255;
                }
                rgb = (alpha << 24) | (rgb & 0x00ffffff);
                bufferedImage.setRGB(x, y, rgb);
            }
        }


        /**
         * 对image的每个像素点进行遍历处理，获得每个像素点的rgb颜色。判断当前rgb是否为匹配替换的rgb，如果是就替换成透明
         */
//        int alpha = 0; // 图片透明度
//        // 外层遍历是Y轴的像素
//        for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {
//            // 内层遍历是X轴的像素
//            for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {
//                int rgb = bufferedImage.getRGB(x, y);
//                // 对当前颜色判断是否在指定区间内
//                if (colorInRange(rgb)) {
//                    alpha = 0;
//                } else {
//                    // 设置为不透明
//                    alpha = 255;
//                }
//                // #AARRGGBB 最前两位为透明度
//                rgb = (alpha << 24) | (rgb & 0x00ffffff);
//                bufferedImage.setRGB(x, y, rgb);
//            }
//        }
        // 绘制设置了RGB的新图片
        g2D.drawImage(bufferedImage, 0, 0, null);
        //转换类型
        MultipartFile multipartFile = fileCase(bufferedImage);
        return multipartFile;
    }

    public static String convertRgb(int color) {
        int red = (color & 0xff0000) >> 16;// 获取color(RGB)中R位
        int green = (color & 0x00ff00) >> 8;// 获取color(RGB)中G位
        int blue = (color & 0x0000ff);// 获取color(RGB)中B位
        return red + "," + green + "," + blue;
    }

    
    // 判断是背景还是内容
    public static boolean colorInRange(int color) {
        int red = (color & 0xff0000) >> 16;// 获取color(RGB)中R位
        int green = (color & 0x00ff00) >> 8;// 获取color(RGB)中G位
        int blue = (color & 0x0000ff);// 获取color(RGB)中B位
        // 通过RGB三分量来判断当前颜色是否在指定的颜色区间内
        if (red >= color_range && green >= color_range && blue >= color_range) {
            return true;
        }
        return false;
    }

    public static BufferedImage getBufferedImage(@RequestParam MultipartFile file) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(file.getInputStream());
        } catch (IOException e) {
            System.out.println("读取图片文件出错！" + e.getMessage());
        }
        return image;
    }

    public static MultipartFile fileCase(BufferedImage image) {
        //得到BufferedImage对象
        MultipartFile multipartFile = null;
        try {
            //创建一个ByteArrayOutputStream
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            //把BufferedImage写入ByteArrayOutputStream
            ImageIO.write(image, "png", os);
            //ByteArrayOutputStream转成InputStream
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            //InputStream转成MultipartFile
            multipartFile = new MockMultipartFile("file", "file.png", "text/plain", input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return multipartFile;

    }

}

